/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
UART_IT_TX_Def Uart1TxBuffer;

/* Private function prototypes -----------------------------------------------*/
static void UART_Config(void);
static void UART1_IT_Send(uint8_t *pData, uint16_t len);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */
  /* Initialize LEDs available on HK32G003F4P7_EVAL board ************************/
  HK_EVAL_LEDInit(LED1);

  /* UART configuration */
  UART_Config();

  /* Set UART1 TxBuffer Status */
  Uart1TxBuffer.FinishedFlag = SET;

  while(1)
  {
    /* Test */
    if( Uart1TxBuffer.FinishedFlag == SET )
    {
      HK_EVAL_LEDOn(LED1);
      UART1_IT_Send("UART1 IT Send String Testing is OK!\r\n", sizeof("UART1 IT Send String Testing is OK!\r\n") - 1);
    }
  }
}

/**
  * @brief  Configure the UART Device
  * @param  None
  * @retval None
  */
static void UART_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  UART_InitTypeDef UART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable GPIO clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);

  /* Enable USART clock */
  RCC_APBPeriph2ClockCmd(RCC_APBPeriph2_UART1, ENABLE);

  /* Connect PXx to UARTx_Tx */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

  /* Connect PXx to UARTx_Rx */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_1);

  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure UART Rx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* UARTx configured as follow:
  - BaudRate = 115200 baud
  - Word Length = 8 Bits
  - Stop Bit = 1 Stop Bit
  - Parity = No Parity
  - Hardware flow control disabled (RTS and CTS signals)
  - Receive and transmit enabled
  */
  UART_InitStructure.UART_BaudRate = 115200;
  UART_InitStructure.UART_WordLength = UART_WordLength_8b;
  UART_InitStructure.UART_StopBits = UART_StopBits_1;
  UART_InitStructure.UART_Parity = UART_Parity_No;
  UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
  UART_Init(UART1, &UART_InitStructure);

  /* Configre NVIC */
  NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable UART */
  UART_Cmd(UART1, ENABLE);
}

/**
  * @brief  UART Send data with TXE intterupt.
  * @param  None
  * @retval None
  */
static void UART1_IT_Send(uint8_t *pData, uint16_t len)
{
  unsigned char i;

  Uart1TxBuffer.Len = len;
  Uart1TxBuffer.Index = 0;
  Uart1TxBuffer.FinishedFlag = RESET;

  /* Copy data to transfer buffer */
  for( i = 0; i < len; i ++ )
  {
    Uart1TxBuffer.Data[i] = *(pData ++ );
  }

  /* Enable transfer empty interrupt */
  UART_ITConfig(UART1, UART_IT_TXE, ENABLE);

  /* Enable transfer complete interrupt */
  UART_ITConfig(UART1, UART_IT_TC, DISABLE);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


